package com.ms.content.lingten;

import com.rabbitmq.client.Channel;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Component
public class RabbitMqJt {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private StringRedisTemplate stringredisTemplate;

    @RabbitListener(queues = "user_phone")
    @RabbitHandler
    public void mqPhone(String core, Message message, Channel channel) throws IOException {

        String code ="";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            code  += random.nextInt(10);
        }
        System.out.println("验证码为"+code);

        //存入redis 并设置5分钟的过期时间
        stringredisTemplate.opsForValue().set(core,code,5, TimeUnit.MINUTES);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
